#include "HUDControl.h"
#include "GameManager.h"
#include "Ship.h"
#include <windows.h>
#include "GuiDebugWindow.h"

CHUDControl::CHUDControl(CGameManager *engine)
{
	this->mEngine = engine;

	dimension2d<s32> dim = this->mEngine->getVideoDriver()->getScreenSize();

	rect<s32> corner;
	corner.UpperLeftCorner.X = 0;
	corner.UpperLeftCorner.Y = dim.Height - 200;
	corner.LowerRightCorner.X = 200;
	corner.LowerRightCorner.Y = dim.Height; 
	mCompassX = new CCompass(corner, engine->getGuiManager(), engine->getGuiManager()->getRootGUIElement());
	mCompassX->SetCompassBodyTexture(engine->getVideoDriver()->getTexture("Content/compass/compass_body.tga"));
	mCompassX->SetCompassNeedleTexture(engine->getVideoDriver()->getTexture("Content/compass/compass_needle.tga"));

	corner.UpperLeftCorner.X = 200;
	corner.UpperLeftCorner.Y = dim.Height - 200;
	corner.LowerRightCorner.X = 400;
	corner.LowerRightCorner.Y = dim.Height; 
	mCompassY = new CCompass(corner, engine->getGuiManager(), engine->getGuiManager()->getRootGUIElement());
	mCompassY->SetCompassBodyTexture(engine->getVideoDriver()->getTexture("Content/compass/compass_body.tga"));
	mCompassY->SetCompassNeedleTexture(engine->getVideoDriver()->getTexture("Content/compass/compass_needle.tga"));

#ifdef _DEBUG
	mDebugWindow = new CGuiDebugWindow();
#endif
}

CHUDControl::~CHUDControl(void)
{
}


void CHUDControl::drawAll(void) {
	IVideoDriver *driver = mEngine->getVideoDriver();
	WCHAR buff[1024];
	CSceneObject *obj;

	IGUIFont *font = this->mEngine->getGuiManager()->getFont("Content/myfont.xml");

	dimension2d<s32> dim = driver->getScreenSize();
	swprintf(buff, L"FPS: %d", driver->getFPS());
	font->draw(buff, core::rect<s32>(dim.Width - 75,30,30,30), video::SColor(255,255,255,255));
	
	swprintf(buff, L"Time: %f", mEngine->getDevice()->getTimer()->getTime() / 1000.0f);
	font->draw(buff, core::rect<s32>(position2d<s32>(0,dim.Height-300), dimension2d<s32>(30,200)), video::SColor(255,255,255,255));

	// draw selection stuff
	if(mEngine->getSelection().size() == 0)
		return;
	
	// ghetto write ship info
	obj = mEngine->getSelection()[0];
	if(obj->getType() == E_GAMEOBJECT_TYPE::Ship) { 
		CShip *ptr = (CShip *)obj;
		// get degrees too
		vector3df vec;
		wchar_t wtf[1024];
		s32 a, b, c;

		this->mCompassX->SetCompassHeading(ptr->getRotation().getHorizontalAngle().X);
		this->mCompassY->SetCompassHeading(ptr->getRotation().getHorizontalAngle().Y);

		a = b = 30;
		c = 300;
		swprintf(buff, L"ID: %08x", ptr->getID());
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));

		a+=20;
		MultiByteToWideChar(CP_ACP, 0, ptr->getName(), -1,  wtf, 1024);
		swprintf(buff, L"Ship: %s", wtf);
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));

		a+=20;
		vec = ptr->getPosition();
		swprintf(buff, L"Position: %f %f %f", vec.X, vec.Y, vec.Z);
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));

		a+=20;
		vec = ptr->getVelocity();
		swprintf(buff, L"Velocity: %f %f %f", vec.X, vec.Y, vec.Z);
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));

		a+=20;
		vec = ptr->getRotation();
		swprintf(buff, L"Rotation: %f %f %f", vec.X, vec.Y, vec.Z);
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));

		a+=20;
		vec = ptr->getVelocity();
		swprintf(buff, L"Speed: %f", 0);
		font->draw(buff, core::rect<s32>(b,a,b,c), video::SColor(255,255,255,255));
	}


}
